package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.StorageCellDto;
import com.guigu.erp.mapper.StorageCellMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lenovo
 */
@Service
public class StorageCellServiceImpl extends ServiceImpl<StorageCellMapper, StorageCell> implements StorageCellService {
    @Autowired
    private StorageCellMapper storageCellMapper;

    @Autowired
    private StorageCellService storageCellService;

    @Autowired
    private DesignFileService designFileService;

    /**
     * 获取Id最大库存配置单的库存编号
     * @return
     */
    @Override
    public String getMaxStoreId() {
        StorageCell storageCell=storageCellMapper.queryMaxStoreId();
        if (storageCell==null){
            return null;
        }
        // 获取库存配置单库存配置单编号
        String storeId=storageCell.getStoreId();
        return storeId;
    }

    @Override
    public void addStorageCell(StorageCellDto storageCellDto) {
        //1.插入库存安全配置单
        this.save(storageCellDto);

        //2.修改设计标志
        String productId=storageCellDto.getProductId();
        QueryWrapper<DesignFile> wrapper=new QueryWrapper<>();
        wrapper.eq("product_id",productId);
        DesignFile designFile=designFileService.getOne(wrapper);
        designFile.setDesignCellTag("K001-1");
        designFileService.updateById(designFile);
    }

    @Override
    public PageUtils<StorageCell> recheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<StorageCell> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("first_kind_id",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("third_Kind_Id",vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("register_time",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("register_time",vo.getSearch_registerTime2());
            }
        }
        // 额外添加1个条件
        // 审核状态：审核未通过
        queryWrapper.eq("check_tag","S001-0");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<StorageCell> storageCellList =this.list(queryWrapper);
        PageInfo<StorageCell> storageCellPageInfo = new PageInfo<>(storageCellList);

        //将pageinfo 转换成 pageutils
        PageUtils<StorageCell> pageUtils = new PageUtils<>();
        pageUtils.setRows(storageCellPageInfo.getList());
        pageUtils.setTotal(storageCellPageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<StorageCell> storageCellListPage(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<StorageCell> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("first_kind_id",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("third_Kind_Id",vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("register_time",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("register_time",vo.getSearch_registerTime2());
            }
        }
        // 额外添加1个条件
        // 审核状态：审核未通过
        queryWrapper.eq("check_tag","S001-1");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<StorageCell> storageCellList =this.list(queryWrapper);
        PageInfo<StorageCell> storageCellPageInfo = new PageInfo<>(storageCellList);

        //将pageinfo 转换成 pageutils
        PageUtils<StorageCell> pageUtils = new PageUtils<>();
        pageUtils.setRows(storageCellPageInfo.getList());
        pageUtils.setTotal(storageCellPageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public String cellListById(int id) {
        String storageCell=storageCellMapper.cellListById(id);
        return storageCell;
    }

    @Override
    public PageUtils<StorageCell> ListByProductId(String productId) {
        QueryWrapper<StorageCell> wrapper=new QueryWrapper<StorageCell>();
        wrapper.eq("product_id",productId);

        List<StorageCell> storageCell=this.list(wrapper);
        PageInfo<StorageCell> pageInfo=new PageInfo<StorageCell>(storageCell);

        PageUtils<StorageCell> pageUtils = new PageUtils<StorageCell>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }
}
